package org.example;

import org.apache.ibatis.session.SqlSession;
import org.example.mapper.StudentMapper;
import org.example.model.Student;
import org.example.util.MybatisUtil;
import org.junit.Test;

import java.util.List;

public class Text {

    @Test
    public void selectAllStudents() {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectAllStudent();
        students.forEach(System.out::println);
        MybatisUtil.closeSqlSession(sqlSession);
    }
    @Test
    public void selectStudentById() {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectStudentById(1);
        System.out.println(student);
        MybatisUtil.closeSqlSession(sqlSession);
    }
    @Test
    public void deleteStudentById() {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int i = mapper.deleteStudentById(1);
        System.out.println("影响数据条数:"+i);
        MybatisUtil.closeSqlSession(sqlSession);
    }
    @Test
    public void updateStudentById() {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setId(2);
        student.setName("张三");
        student.setAge(20);
        int i = mapper.updateStudentById(student);
        System.out.println("影响数据条数:"+i);
        MybatisUtil.closeSqlSession(sqlSession);
    }
    /*
    select * from student where name like "%马%"
    select * from student where age>20 order by age,math desc;
    select count(id) 总人数 max(age) 最大年龄 min(age) 最小年龄 avg(age) 平均年龄
    from student
    group by address
    having count(id)>2
    order by count(id) desc
    limit 3;
    select * from student where math>80 and english>72;
    select address from student where name="刘德华";
    select * from student where address=(select address from student where name="刘德华");
    */
}
